package groups.diseases.gui;

import groups.diseases.Disease;
import groups.diseases.DiseaseMain;
import groups.diseases.HumanSymptom;
import groups.diseases.etapa2.Data;
import groups.diseases.etapa2.GoogleChartApi;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;

public class JDiseaseFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8223805790486012986L;
	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel chartPanel1;
	private JPanel chartPanel2;
	private JTextField diseaseName;
	private JTable diseasesSymptomTable;
	private DiseaseMain main;
	private JComboBox diseasesComboBox;
	private JTextField symptomDescription;
	private JTable symptomTable;
	private JComboBox symptomsComboBox;
	private JCheckBox chckbxEndemic;
	private JComboBox diseaseOrSymptomComboBox;
	private JComboBox valuesCB;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JDiseaseFrame frame = new JDiseaseFrame(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JDiseaseFrame(DiseaseMain m) {
		main = m;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 580, 415);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(12, 12, 548, 363);
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Doen\u00E7as", null, panel, null);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Nome");
		lblNewLabel.setBounds(12, 50, 71, 18);
		panel.add(lblNewLabel);
		
		diseaseName = new JTextField();
		diseaseName.setBounds(95, 45, 222, 28);
		panel.add(diseaseName);
		diseaseName.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 167, 512, 140);
		panel.add(scrollPane);
		
		diseasesSymptomTable = new JTable();
		scrollPane.setViewportView(diseasesSymptomTable);
		
		
		final DefaultTableModel model=(DefaultTableModel) diseasesSymptomTable.getModel();
		model.addColumn("Sintomas");
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Sintomas", null, panel_1, null);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(12, 84, 512, 223);
		panel_1.add(scrollPane_1);
		
		symptomTable = new JTable();
		scrollPane_1.setViewportView(symptomTable);
		
		final DefaultTableModel model2 = (DefaultTableModel) symptomTable.getModel();
		model2.addColumn("Sintomas");
		
		symptomTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				if(SwingUtilities.isRightMouseButton(e)){
					int row = symptomTable.rowAtPoint(e.getPoint());
					
					HumanSymptom hs = (HumanSymptom) symptomTable.getValueAt(
							row , 0);
					
					if(JOptionPane.showConfirmDialog(null, "Are you sure you want delete this symptom?")==JOptionPane.YES_OPTION){
						main.deleteHumanSymptom(hs);
						((DefaultTableModel)symptomTable.getModel()).removeRow(row);
					}
				}
			}
		});
		
		JLabel lblNome = new JLabel("Descri\u00E7\u00E3o");
		lblNome.setBounds(12, 12, 71, 18);
		panel_1.add(lblNome);
		
		symptomDescription = new JTextField();
		symptomDescription.setBounds(95, 7, 207, 28);
		panel_1.add(symptomDescription);
		symptomDescription.setColumns(10);
		
		JButton createSymptomButtom = new JButton("Criar");
		
		createSymptomButtom.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				String des  = symptomDescription.getText().trim();
				if(des.compareTo("")!=0){
					main.saveSymptom(-1, des);
					main.fillFrame();
				}
			}
		});
		
		createSymptomButtom.setBounds(95, 47, 97, 30);
		panel_1.add(createSymptomButtom);

		
		diseasesSymptomTable.setEnabled(false);
		diseasesSymptomTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				if(SwingUtilities.isRightMouseButton(e)){
					int row = diseasesSymptomTable.rowAtPoint(e.getPoint());
					
					HumanSymptom hs = (HumanSymptom) diseasesSymptomTable.getValueAt(
							row ,
							diseasesSymptomTable.columnAtPoint(e.getPoint()));
					
					if(JOptionPane.showConfirmDialog(null, "Are you sure you want delete this symptom?")
							==JOptionPane.YES_OPTION){						
						((DefaultTableModel)diseasesSymptomTable.getModel()).removeRow(row);
					}
				}
			}
		});
		
		
		JButton createDiseaseButton = new JButton("Criar");
		createDiseaseButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				Disease d = main.saveDisease(-1, diseaseName.getText().trim(),chckbxEndemic.isSelected(), getCurrentDiseasesSymptoms());
				main.fillFrame();
				diseasesComboBox.setSelectedItem(d);
			}
		});
		
		createDiseaseButton.setBounds(12, 125, 97, 30);
		panel.add(createDiseaseButton);
		
		JLabel lblDoenas = new JLabel("Doen\u00E7as");
		lblDoenas.setBounds(12, 12, 71, 18);
		panel.add(lblDoenas);
		
		diseasesComboBox = new JComboBox();
		diseasesComboBox.setBounds(95, 7, 222, 28);
		panel.add(diseasesComboBox);
		diseasesComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED){
//					clearEverything();
					DefaultTableModel model =(DefaultTableModel)diseasesSymptomTable.getModel(); 
					while(model.getRowCount()>0)
						model.removeRow(model.getRowCount()-1);
					main.loadDisease(((Disease)e.getItem()).getId());
					main.loadDiseaseCausesSymptom(((Disease)e.getItem()).getId());
					
				}
			}
		});
		
		JButton btnEditar = new JButton("Salvar");
		btnEditar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Disease d = (Disease)diseasesComboBox.getSelectedItem();
				main.saveDisease(d.getId(), diseaseName.getText(), chckbxEndemic.isSelected(),getCurrentDiseasesSymptoms());
				main.fillFrame();
				main.loadDisease(d.getId());
			}
		});
		btnEditar.setBounds(121, 125, 97, 30);
		panel.add(btnEditar);
		
		JButton btnDeletarDoena = new JButton("Deletar Doen\u00E7a");
		btnDeletarDoena.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				main.deleteDisease((Disease)diseasesComboBox.getSelectedItem());
				main.fillFrame();
			}
		});
		btnDeletarDoena.setBounds(324, 6, 130, 30);
		panel.add(btnDeletarDoena);
		
		symptomsComboBox = new JComboBox();
		symptomsComboBox.setBounds(95, 85, 222, 28);
		panel.add(symptomsComboBox);
		
		JLabel lblSintomas = new JLabel("Sintomas");
		lblSintomas.setBounds(12, 90, 71, 18);
		panel.add(lblSintomas);
		
		JButton btnAdicionar = new JButton("Adicionar");
		btnAdicionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addDiseaseCausesSymptom((HumanSymptom) symptomsComboBox.getSelectedItem());
			}
		});
		btnAdicionar.setBounds(324, 84, 97, 30);
		panel.add(btnAdicionar);
		
		chckbxEndemic = new JCheckBox("Endemic?");
		chckbxEndemic.setBounds(321, 48, 133, 22);
		panel.add(chckbxEndemic);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Estat\u00EDsticas", null, panel_2, null);
		panel_2.setLayout(null);
		
		valuesCB = new JComboBox();
		valuesCB.setBounds(172, 12, 213, 28);
		panel_2.add(valuesCB);
		
		diseaseOrSymptomComboBox = new JComboBox();
		diseaseOrSymptomComboBox.setBounds(12, 12, 148, 28);
		panel_2.add(diseaseOrSymptomComboBox);
		diseaseOrSymptomComboBox.addItem("Sintoma");
		diseaseOrSymptomComboBox.addItem("Doença");
		
		diseaseOrSymptomComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				valuesCB.removeAllItems();
				
				if(e.getItem().toString() == "Sintoma"){
					for (int i = 0; i < symptomsComboBox.getItemCount(); i++) {
						valuesCB.addItem(symptomsComboBox.getItemAt(i));
					}
				}
				else{
					for (int i = 0; i < diseasesComboBox.getItemCount(); i++) {
						valuesCB.addItem(diseasesComboBox.getItemAt(i));
					}
				}
			}
		});
		
//		diseaseOrSymptomComboBox.setSelectedIndex(1);
		
		final JTextArea lblConteudo = new JTextArea("Conteudo");
		lblConteudo.setBounds(12, 64, 512, 93);
		panel_2.add(lblConteudo);
		
		chartPanel1 = new JPanel();
		tabbedPane.addTab("Gr\u00E1fico", null, chartPanel1, null);
		
		chartPanel2 = new JPanel();
		tabbedPane.addTab("Grafo", null, chartPanel2, null);
		
		valuesCB.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED){
//					System.out.println(e.getStateChange()+" " +diseaseOrSymptomComboBox.getSelectedIndex());
					if(valuesCB.getSelectedItem() instanceof Disease){//Doença
						System.out.println("Shit1");
						lblConteudo.setText(main.getDiseaseInfo((Disease)e.getItem()));
					}else if(valuesCB.getSelectedItem() instanceof HumanSymptom) {
						System.out.println("Shit2");
						lblConteudo.setText(main.getSymptomInfo((HumanSymptom)e.getItem()));
					}

						
				}
			}
		});
		
		
	}

	public void addDiseaseList(Disease d) {
		diseasesComboBox.addItem(d);
	}

	public void addSymptomList(HumanSymptom h) {
		((DefaultTableModel)symptomTable.getModel()).addRow(new Object[]{h});
		symptomsComboBox.addItem(h);
	}



	public void addDiseaseCausesSymptom(HumanSymptom c) {
		((DefaultTableModel)diseasesSymptomTable.getModel()).addRow(new Object[]{c});
	}

	public void setDisease(Disease d) {
		diseaseName.setText(d.getName());
		chckbxEndemic.setSelected(d.isEndemic());
	}

	public void setSymptom(HumanSymptom h) {
		symptomDescription.setText(h.getDescription());
	}
	
	@SuppressWarnings("unchecked")
	public Vector<HumanSymptom> getCurrentDiseasesSymptoms(){
		Vector<HumanSymptom> v = new Vector<HumanSymptom>();
		
		for (Vector<HumanSymptom> row : (Vector<Vector<HumanSymptom>>) ((DefaultTableModel)diseasesSymptomTable.getModel()).getDataVector()) {
			v.add(row.elementAt(0));
		}
		
		return v;
	}
	
	public void clearEverything(){
		diseasesComboBox.removeAllItems();
		symptomsComboBox.removeAllItems();
		DefaultTableModel model =(DefaultTableModel)diseasesSymptomTable.getModel(); 
		while(model.getRowCount()>0)
			model.removeRow(0);
		
		model = ((DefaultTableModel)symptomTable.getModel());
		while(model.getRowCount()>0)
			model.removeRow(0);
		
	}
	
	public JTabbedPane getTabbedPane() {
		return this.tabbedPane;
	}
	
	
	public JPanel getChartPanel1() {
		return this.chartPanel1;
	}
	
	public JPanel getChartPanel2() {
		return this.chartPanel2;
	}
}
